package com.crm.system.mapper;

import com.crm.dto.PartnerCount;
import com.crm.entity.Role;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface SysRoleMapper {

    @Delete("DELETE FROM crm_role\n" +
            "WHERE role_id='#{RoleId}';")
    int Delete(String RoleId);

    @Insert("INSERT INTO crm_role\n" +
            "(role_id, role_name, app_name)\n" +
            "VALUES('#{RoleId}', '#{RoleName}', '#{AppName}');")
    int Insert(Role role);

    @Select("SELECT * From crm_role order by role_id")
    List<Role> list();

    @Select("<script>" +
            "select su.user_id,su.true_name ,count(*) counts \n" +
            "from crm_partner cm\n" +
            "         left join sys_user su\n" +
            "                   on cm.create_user_id = su.user_id\n" +
            "where dep_id = #{depId}\n" +
            "  and cm.disabled = 0\n" +
            "  and su.disabled = 0 " +
            "<if test='beginTime !=null and beginTime!=\"\"'>" +
            " and cm.create_time &gt;= #{beginTime}" +
            "</if>" +
            "<if test='endTime !=null and endTime!=\"\"'>" +
            " and cm.create_time &lt;= #{endTime}" +
            "</if>" +
            " group by su.user_id" +
            "</script>")
    List<PartnerCount> partnerCount(String depId, String beginTime, String endTime);

    @Select("select dep_id from sys_user where user_id=#{userId}")
    String depId(String userId);
}
